package DataBaseUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;

import peng.yang.express.Bean.PhoneMembers;

/**
 * Created by Administrator on 2016/6/28.
 */
public class SQLLiteHelperPhone extends SQLiteOpenHelper {

    private static final String DB_NAME = "ExpressPhone";
    private static final int DB_VERSION = 1;
    private static final String TABLE_NAME = "ExpressPhoneTable";
    private static final String COL_expName = "expName";
    private static final String COL_phone = "phone";
    private static final String COL_netImageUrl = "netImageUrl";
    private static final String COL_fileImagePath = "fileImagePath";

    String sql = "CREATE TABLE " + TABLE_NAME
            + "(netImageUrl VARCHAR(20),"
            + " expName VARCHAR(20),"
            + " phone VARCHAR(20),"
            + " simpleName VARCHAR(20),"
            + " fileImagePath VARCHAR(20))";

    //构造函数，创建数据库
    public SQLLiteHelperPhone(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    //建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sql);
    }

    //更新数据库的时候调用
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
    }
}

  /*  //    获得数据库所有的数据
    public List<PhoneMembers> getAllMembers() {
        List<PhoneMembers> memberList = new ArrayList<>();
        SQLiteDatabase database = getReadableDatabase();
//        要查询的字段
        String[] columns = {COL_netImageUrl, COL_expName, COL_phone, COL_fileImagePath};
//        调用query查询
        Cursor cursor = database.query(TABLE_NAME, columns, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String netImageUrl = cursor.getString(cursor.getColumnIndex(COL_netImageUrl));
            String expName = cursor.getString(cursor.getColumnIndex(COL_expName));
            String phone = cursor.getString(cursor.getColumnIndex(COL_phone));
            String fileImagePath = cursor.getString(cursor.getColumnIndex(COL_fileImagePath));

            PhoneMembers member = new PhoneMembers();
            member.setNetImageUrl(netImageUrl);
            member.setExpName(expName);
            member.setPhone(phone);
            member.setFileImagePath(fileImagePath);

            memberList.add(member);
        }
        return memberList;
    }

//    根据条件获得数据库信息

    public PhoneMembers findById(String COL_expName) {
        SQLiteDatabase db = getWritableDatabase();
        String[] columns = {COL_netImageUrl, COL_expName, COL_phone, COL_fileImagePath};
        String selection = COL_expName + " = ?;";
        String[] selectionArgs = {COL_expName};
        Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs,
                null, null, null);
        PhoneMembers member = null;
        if (cursor.moveToNext()) {
            String netImageUrl = cursor.getString(cursor.getColumnIndex(COL_netImageUrl));
            String expName = cursor.getString(cursor.getColumnIndex(COL_expName));
            String phone = cursor.getString(cursor.getColumnIndex(COL_phone));
            String fileImagePath = cursor.getString(cursor.getColumnIndex(COL_fileImagePath));
            member = new PhoneMembers();
            member.setNetImageUrl(netImageUrl);
            member.setExpName(expName);
            member.setPhone(phone);
            member.setFileImagePath(fileImagePath);
        }
        cursor.close();
        return member;
    }

}*/

   /* *//**
     *
     * @param member
     * @return
     *//*
    public int update(ExpressMember member) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("COL_num",member.getNum());
        cv.put("COL_expressName", member.getExpressname());
        cv.put("COL_ImageId", member.getImageId());
        cv.put("COL_goodName", member.getGoodname());
        String whereClause = COL_num + " = ?;";
        String[] whereArgs = {member.getNum()};
        return db.update(TABLE_NAME, cv, whereClause, whereArgs);
    }


    public int deleteById(String COL_num) {
        SQLiteDatabase db = getWritableDatabase();
        String whereClause = COL_num + " = ?;";
//        字符串变为整形数为String.valueOf(id)
        String[] whereArgs = {COL_num};
        return db.delete(TABLE_NAME, whereClause, whereArgs);
    }

}*/
